/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#include "scene_panel.h"

#include "common/scene.h"
#include "object/object.h"
#include "light/light.h"

#include <QMenu>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>

namespace Ui {

Scene_Panel::Scene_Panel(QWidget* _parent = 0,Scene_Tree* _scene_tree = 0,Sirat::Common::Scene* _scene = 0) : QWidget(_parent) ,scene_tree(_scene_tree) , scene(_scene) , selected_object(0), id_selected_object(-1) {
	
	QVBoxLayout* vlayout = new QVBoxLayout();	
	QHBoxLayout* hlayout = new QHBoxLayout();
	
	hlayout->addStretch();
	add_object_button = new QPushButton(tr("+ Object"));
	hlayout->addWidget(add_object_button);
	
	hlayout->addStretch();
	
	add_light_button = new QPushButton(tr("+ Light"));
	hlayout->addWidget(add_light_button);
	
	hlayout->addStretch();
	
	remove_object_button = new QPushButton(tr("- Object"));
	hlayout->addWidget(remove_object_button);
	hlayout->addStretch();
	
	QMenu* object_menu = create_object_menu();
	
	add_object_button->setMenu(object_menu);
	
	QMenu* light_menu = create_light_menu();
	
	add_light_button->setMenu(light_menu);
	
	
	vlayout->addLayout(hlayout);
	vlayout->addStretch();
	setLayout(vlayout);

	connect(this,SIGNAL(scene_updated()),scene_tree,SLOT(reload_tree()));
	connect(remove_object_button,SIGNAL(clicked()),this,SLOT(remove_object()));
}

Scene_Panel::~Scene_Panel(void) { 
	delete remove_object_button;
}

QSize Scene_Panel::minimumSizeHint() const {
	return QSize(250,25);
}

void Scene_Panel::set_scene(Sirat::Common::Scene* _scene) {
	scene = _scene;
}

void Scene_Panel::add_sphere(void) {
	Sirat::Object::Object* new_sphere = new Sirat::Object::Sphere();
	scene->add_object(new_sphere);
	emit scene_updated();
}
void Scene_Panel::add_plane(void) {
	Sirat::Object::Object* new_plane = new Sirat::Object::Plane();
	scene->add_object(new_plane);
	emit scene_updated();
}
void Scene_Panel::add_cone(void) {
	Sirat::Object::Object* new_cone = new Sirat::Object::Cone();
	scene->add_object(new_cone);
	emit scene_updated();
}

void Scene_Panel::add_cylinder(void) {
	Sirat::Object::Object* new_cylinder = new Sirat::Object::Cylinder();
	scene->add_object(new_cylinder);
	emit scene_updated();
}

void Scene_Panel::add_disc(void) {
	Sirat::Object::Object* new_disc = new Sirat::Object::Disc();
	scene->add_object(new_disc);
	emit scene_updated();
}

void Scene_Panel::add_triangle(void) {
	Sirat::Object::Object* new_triangle = new Sirat::Object::Triangle();
	scene->add_object(new_triangle);
	emit scene_updated();
}

void Scene_Panel::add_quadrilateral(void) {
	Sirat::Object::Object* new_quadrilateral = new Sirat::Object::Quadrilateral();
	scene->add_object(new_quadrilateral);
	emit scene_updated();
}

void Scene_Panel::add_punctual_light(void) {
	Sirat::Light::Light* new_punctual_light = new Sirat::Light::Punctual_Light();
	scene->add_light(new_punctual_light);
	emit scene_updated();
}

void Scene_Panel::add_square_light(void) {
	Sirat::Light::Light* new_square_light = new Sirat::Light::Square_Light();
	scene->add_light(new_square_light);
	emit scene_updated();
}

void Scene_Panel::remove_object(void) {
	if(id_selected_object != -1 && selected_object != 0) {
		scene->remove_object(id_selected_object);
		selected_object = 0;
		id_selected_object = -1;
		emit scene_updated();
	}
}

void Scene_Panel::select_object(Sirat::Object::Object* _object,int _id_object) {
	selected_object = _object;
	id_selected_object = _id_object;
}

QMenu* Scene_Panel::create_object_menu(void) {
	QMenu* object_menu = new QMenu(add_object_button);
	QAction* sphere_menu_item = new QAction(tr("Sphere"),object_menu);
	QAction* plane_menu_item = new QAction(tr("Plane"),object_menu);
	QAction* cone_menu_item = new QAction(tr("Cone"),object_menu);
	QAction* cylinder_menu_item = new QAction(tr("Cylinder"),object_menu);
	QAction* disc_menu_item = new QAction(tr("Disc"),object_menu);
	QAction* triangle_menu_item = new QAction(tr("Triangle"),object_menu);  
	QAction* quadrilateral_menu_item = new QAction(tr("Quadrilateral"),object_menu);  
	
	object_menu->addAction(sphere_menu_item);
	object_menu->addAction(plane_menu_item);
	object_menu->addAction(cone_menu_item);
	object_menu->addAction(cylinder_menu_item);
	object_menu->addAction(disc_menu_item);
	object_menu->addAction(triangle_menu_item);
	object_menu->addAction(quadrilateral_menu_item);
	
	
	connect(sphere_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_sphere()));
	connect(plane_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_plane()));
	connect(cone_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_cone()));
	connect(cylinder_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_cylinder()));
	connect(disc_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_disc()));
	connect(triangle_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_triangle()));
	connect(quadrilateral_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_quadrilateral()));
	
	return object_menu;
}

QMenu* Scene_Panel::create_light_menu(void) {
	QMenu* light_menu = new QMenu(add_light_button);
	QAction* punctual_light_menu_item = new QAction(tr("Punctual"),light_menu);
	QAction* square_light_menu_item = new QAction(tr("Square"),light_menu);
		
	light_menu->addAction(punctual_light_menu_item);
	light_menu->addAction(square_light_menu_item);
	
	
	connect(punctual_light_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_punctual_light()));
	connect(square_light_menu_item,SIGNAL(triggered(bool)),this,SLOT(add_square_light()));
	
	return light_menu;
}

}
